package com.slipper.groovy.util;

import com.slipper.PaasContext;
import com.slipper.system.dictionary.bo.DictionaryDataBO;
import com.slipper.system.dictionary.service.DictionaryDataService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DictionaryUtil {
    public static List<Map<String, Object>> getDictList(String dictName) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<DictionaryDataBO> dataList = PaasContext.getBean(DictionaryDataService.class).queryByDictName(dictName);
        dataList.forEach(data -> {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("dict_parent", data.getDictParent());
            resultMap.put("dict_key", data.getDictKey());
            resultMap.put("dict_value", data.getDictValue());
            resultMap.put("ordinal", data.getOrdinal());
            resultList.add(resultMap);
        });
        return resultList;
    }

    public static Map<String, String> getDictMap(String dictName) {
        List<DictionaryDataBO> dataList = PaasContext.getBean(DictionaryDataService.class).queryByDictName(dictName);
        Map<String, String> resultMap = new HashMap<>();
        dataList.forEach(data -> resultMap.put(data.getDictKey(), data.getDictValue()));
        return resultMap;
    }

    public static Map<String, String> getReverseMap(String dictName) {
        List<DictionaryDataBO> dataList = PaasContext.getBean(DictionaryDataService.class).queryByDictName(dictName);
        Map<String, String> resultMap = new HashMap<>();
        dataList.forEach(data -> resultMap.put(data.getDictValue(), data.getDictKey()));
        return resultMap;
    }
}
